﻿<!DOCTYPE html>
<html>
<head>
<!--
  Copyright (c) 2015 Jean-Marc VIGLINO, 
  released under CeCILL-B (french BSD like) licence: http://www.cecill.info/
 -->
  <title>ol-ext: DBPedia layer</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

  <meta name="description" content="DBPedia layer for OL3" />
  <meta name="keywords" content="ol3, layer, source, vector, wikipedia, dbpedia" />
  
  <!-- jQuery -->
  <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.0.min.js"></script>

  <!-- Openlayers -->
  <link rel="stylesheet" href="https://openlayers.org/en/v6.15.1/css/ol.css" />
  <script type="text/javascript" src="https://openlayers.org/en/v6.15.1/build/ol.js"></script>

  
  <!-- ol-ext -->
  <link rel="stylesheet" href="../../dist/ol-ext.css" />
  <script type="text/javascript" src="../../dist/ol-ext.js"></script>
  <!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
  <script src="https://unpkg.com/elm-pep"></script>

  <link rel="stylesheet" href="../style.css" />
  <style>
    #select img {
      max-height: 300px;
    }
  </style>
</head>
<body >
  <a href="https://github.com/Viglino/ol-ext" class="icss-github-corner"><i></i></a>

  <a href="../../index.html">
    <h1>ol-ext: DBPedia layer</h1>
  </a>
  <div class="info">
    An example using a BDpedia layer with clusters to show photos on a map..
    <ul>
    <li>
      Use <a href="../layer/map.dbpedia.html">ol.source.DBPedia</a>.
    </li>
    <li>
      Use <a href="../style/map.style.photo.html">ol.style.Photo</a>.
    </li>
    <li>
      Use <a href="../animation/map.animatedcluster.html">AnimatedCluster</a>.
    </li>
    </ul>
  </div>

  <!-- DIV pour la carte -->
  <div id="map" style="width:600px; height:400px;"></div>
  <div class="options"></div>
  <div id ="select" class="options" >Select an item.</div>
  
  <script type="text/javascript">
  // The map
  var map = new ol.Map({
    target: 'map',
    view: new ol.View({
      zoom: 15,
      center: [261204.43490751847, 6250258.191535994]
    }),
    interactions: ol.interaction.defaults(),
    layers: [ new ol.layer.Tile({ name:"OSM", source: new ol.source.OSM() })]
  });

  // Style
  var styleCache={};
  function getFeatureStyle (feature, resolution, sel) {	
    var f = feature.get("features")[0];
    var nb = feature.get("features").length;
    var th = f.get("thumbnail");
    var k = th.replace(/(.*)\/(.*)\?(.*)/,"$2")+(nb>1?"_0":"_1")+(sel==='sel'?"_1":"");
    var style = styleCache[k];
    if (!style) {
      styleCache[k] = style = new ol.style.Style({
        image: new ol.style.Photo ({
          src: th,
          radius: 20,
          crop: true,
          kind: (nb>1) ? "folio":"square",
          shadow: true,
          onload: function() { vector.changed(); },
          stroke: new ol.style.Stroke({
            width: sel==='sel' ? 5 : 3,
            color: sel==='sel' ? 'red' : '#fff'
          })
        })
      });
    }
    if (nb>1){
      var count = new ol.style.Style({
        image: new ol.style.RegularShape({
          points: 12,
          radius: 9,
          displacement: [20,20],
          fill: new ol.style.Fill({
            color: '#fff'
          })
        }),
        text: new ol.style.Text({
          text: nb.toString(),
          font: 'bold 12px helvetica,sans-serif',
          offsetX: 20,
          offsetY: -20,
          fill: new ol.style.Fill({
            color: '#000'
          })
        })
      });
      return [ style, count ];
    }
    else return [style];
  }

  // DBPedia layer source
  var vectorSource = new ol.source.DBPedia({
    // Tile strategy load at zoom 14
    strategy: ol.loadingstrategy.tile(ol.tilegrid.createXYZ({ minZoom: 14, maxZoom: 14, tileSize:512  })),
    // Bbox strategy : reload at each move
    //strategy: ol.loadingstrategy.bbox,
    //maxResolution: 10, // > zoom 14
    // Language
    lang:"fr"
  });

  // Force thumbnail to non optional
  vectorSource.querySubject = function () {
    return "?subject rdfs:label ?label. "
      + "?subject dbo:thumbnail ?thumbnail."
      + "OPTIONAL {?subject dbo:abstract ?abstract} . "
      + "OPTIONAL {?subject rdf:type ?type}";
  }

  // Force layer reload on resolution change 
  map.getView().on('change:resolution', function(evt){
    //vectorSource.clear();
    if (map.getView().getZoom() < 14) $(".options").first().text("Zoom to load data...");
    else $(".options").first().text("");
  });

  var clusterSource = new ol.source.Cluster({
    distance: 60,
    source: vectorSource
  });

  var vector = new ol.layer.AnimatedCluster({
    name: 'DBPedia',
    source: clusterSource,
    // Limit resolution to avoid large area request
    maxResolution: 40, // > zoom 12
    // y ordering
    renderOrder: ol.ordering.yOrdering(),
    // default dbPedia style function
    style: getFeatureStyle
  });

  map.addLayer(vector);

  // Control Select 
  var select = new ol.interaction.Select({
    // select dbPedia style function
    style: function(f,res){return getFeatureStyle(f,res,'sel');}
  })
  map.addInteraction(select);
  // On selected
  select.getFeatures().on(['add','remove'], function(e){
    var info = $("#select").html("");
    if (e.type=="add") {
      var el = e.element.get("features")[0];
      $("<h2>").text(el.get("label")).appendTo(info);
      if (el.get("thumbnail")) $("<img>").attr('src',el.get("thumbnail")).appendTo(info);
      $("<p>").text(el.get("abstract")).appendTo(info);
    }
  });

  </script>
</body>
</html>